package org.niugang.service;

import java.util.Date;
import java.util.List;

import org.niugang.bean.UserQueryBean;
import org.niugang.constant.OAauthServiceConstant;
import org.niugang.dao.UserDao;
import org.niugang.entity.UserEntity;
import org.niugang.utils.DateUtils;
import org.niugang.utils.PinyinUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional // import org.springframework.transaction.annotation.Transactional;
public class UserService implements BaseService<UserEntity, UserQueryBean> {

	@Autowired
	private UserDao userDao;

	@Override
	public List<UserEntity> getList(UserQueryBean s) {
		return userDao.getList(s);
	}

	@Override
	public List<UserEntity> getListByPage(UserQueryBean s) {
		return userDao.getListByPage(s);
	}

	@Override
	public void insert(UserQueryBean s) {
		String name = s.getName();
		s.setJianPin(PinyinUtils.getNameSimplicity(name));
		s.setQuanPin(PinyinUtils.getNamePinyin(name));
		s.setDeleteFlag(OAauthServiceConstant.NO_DELETE_FLAG);
		String format = DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSS);
		s.setVersion(Long.valueOf(format));
		String password = new BCryptPasswordEncoder().encode(s.getPassword());
		s.setPassword(password);
		userDao.insert(s);
	}

	@Override
	public void delete(int id) {
		userDao.delete(id);
	}

	@Override
	public UserEntity getById(int id) {
		return userDao.getById(id);
	}

	@Override
	public int count(UserQueryBean s) {
		return userDao.count(s);
	}

	@Override
	public void update(UserQueryBean s) {
		userDao.update(s);

	}

	@Override
	public UserEntity get(UserQueryBean s) {
		return userDao.get(s);
	}


	public List<UserQueryBean> getListPageByQueryBean(UserQueryBean userQueryBean) {
		return userDao.getListPageByQueryBean(userQueryBean);
	}

	public UserEntity getUserByAccount(String account) {
		
		return userDao.getUserByAccount(account);
	};
}
